package com.example.common_base.net2

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.common_base.net2.support.SingleLiveData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * @ClassName: BaseViewModel
 * @Description: java类作用描述
 * @Author: zdl
 * @Date: 2021/11/5 9:25
 */
class BaseViewModel: ViewModel() {


    val loadingLiveData = SingleLiveData<Boolean>()

    val errorLiveData = SingleLiveData<Throwable>()

    /**
     * @deprecated
     */
    fun launch(
        block: suspend () -> Unit,
        error: suspend (Throwable) -> Unit,
        complete: suspend () -> Unit
    ) {
        loadingLiveData.postValue(true)
        viewModelScope.launch(Dispatchers.IO) {
            try {
                block()
            } catch (e: Exception) {
                error(e)
            } finally {
                complete()
            }
        }
    }
}